﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using CKFinder;
using System.Web.Services;
using System.IO;

public partial class Admin_EditPhukien : System.Web.UI.Page
{
    public PhukienController pPhukienController = new PhukienController();
    DanhmucPhukienController danhmucPhukienController = new DanhmucPhukienController();
    public List<Phukien_Image> phukien;
    private string id;
    protected void Page_Load(object sender, EventArgs e)
    {
        CKFinder.FileBrowser fb = new FileBrowser();
        fb.BasePath = "../ckfinder/";
        fb.SetupCKEditor(ckeditor);
        ddlLoaiPhukien.DataSource = danhmucPhukienController.GetAll();
        ddlLoaiPhukien.DataTextField = "name";
        ddlLoaiPhukien.DataValueField = "danhmuc_phukien_id";
        ddlLoaiPhukien.DataBind();
        id = Request.QueryString["id"];
        phukien = pPhukienController.getImagePhukien(Convert.ToInt32(id));
        if (!IsPostBack)
        {
            
            if (id == "" || id == null || string.IsNullOrWhiteSpace(id.ToString()) || string.IsNullOrEmpty(id.ToString()))
            {

                error();
            }
            else
            {
                
                int idd = Convert.ToInt32(id);
                if (!this.checkId(idd))
                {
                    error();
                }
                else
                {
                    DataTable dt = pPhukienController.GetByID(idd);
                    Phukien pk = new Phukien(dt.Rows[0]);
                    txtName.Text = pk.Name;
                    txtPriceInputs.Text = pk.Price_input+"";
                    txtPriceOutputs.Text = pk.Price_output+"";
                    txtUrl.Text = pk.Url;
                    ddlLoaiPhukien.Items.FindByValue(pk.DanhmucPhukien_id.ToString()).Selected = true;
                    ckeditor.Text = pk.Description;
                    img_phukien.ImageUrl =ResolveUrl("~/")+pk.Image;
                    phukien_value.Value = pk.Image;
                    if(pk.Status)
                    {
                        ddlStatus.SelectedIndex = 0;
                    }
                    else
                    {
                        ddlStatus.SelectedIndex = 1;
                    }
                    
                }

            }
        }
    }
    private bool checkId(int id)
    {
        DataTable dt = pPhukienController.GetByID(id);

        if (dt.Rows.Count > 0)
        {
            return true;
        }
        return false;
    }
    private static void error()
    {
        HttpContext.Current.Response.Redirect("~/Error.aspx");
    }
    [WebMethod]
    public static string deleteImagePhukien(int phukien_image_id,string path)
    {
        string fileUpload =HttpContext.Current.Server.MapPath("~/");
        PhukienController pPhukienController = new PhukienController();
        if(pPhukienController.deleteImagePhukien(phukien_image_id)>0)
        {
            string file = fileUpload + path;
            if(File.Exists(file))
            {
                try
                {
                    File.Delete(fileUpload + path);
                    return "true";
                }
                catch (Exception)
                {
                    return "false";
                }
                
            }
            return "true";
        }
        else
        {
            return "false";
        }
    }
    [WebMethod]
    public static string deleteImageProduct(int product_image_id, string path)
    {
        string fileUpload = HttpContext.Current.Server.MapPath("~/");
        ProductImageController pImage = new ProductImageController();
        if (pImage.deleteImageProduct(product_image_id) > 0)
        {
            string file = fileUpload + path;
            if (File.Exists(file))
            {
                try
                {
                    File.Delete(fileUpload + path);
                    return "true";
                }
                catch (Exception)
                {
                    return "false";
                }

            }
            return "true";
        }
        else
        {
            return "false";
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Phukien pk = new Phukien();
        int idd = Convert.ToInt32(id);
        pk.Phukien_id = idd;
        pk.Name = txtName.Text;
        pk.DanhmucPhukien_id = Convert.ToInt32(ddlLoaiPhukien.SelectedValue);
        pk.Price_input = Convert.ToSingle(txtPriceInputs.Text);
        pk.Price_output = Convert.ToSingle(txtPriceOutputs.Text);
        pk.Url = txtUrl.Text;
        pk.Description = ckeditor.Text;
        pk.Image = phukien_value.Value;

        if (ddlStatus.SelectedIndex == 0)
        {
            pk.Status = true;
        }
        else
        {
            pk.Status = false;
        }
        pk.DateUpdate = DateTime.Now;
        pPhukienController.Update(pk);
        if (phukien_image_value.Value != "")
        {
            string str = phukien_image_value.Value.Substring(1);
            char[] s = { ',' };
            string[] image = str.Split(s);
            foreach (var s1 in image)
            {
                pPhukienController.InsertImage(idd, s1);
            }
        }
        Response.Write("<script>alert('Cập nhật phụ kiện thành công')</script>");
        Server.Transfer(Request.RawUrl);
        
    }
}